<?php
/**
 * Created by PhpStorm.
 * User: root
 * Date: 2017/9/5
 * Time: 15:32
 */

namespace app\admin\controller;


use think\Controller;
use app\admin\model\UserModel;
use think\Request;
use think\Session;

class Login extends Controller
{
    public function index(){

        if (Request::instance()->isPost()){
            //show_bug(password(input('post.password'),'LuckyY'));exit;
            //return ['status'=>'1','msg'=>'用户名或密码不正确，请重新输入！'];

            $name = input('post.userName');
            $pwd = input('post.password');
            $captcha = input('post.captcha');

            if(!$name){
                return ['status'=>1,'msg'=>'请输入用户名！'];
            }
            if(!$pwd){
                return ['status'=>1,'msg'=>'请输入密码！'];
            }
            if(!$captcha){
                return ['status'=>1,'msg'=>'请输入验证码！'];
            }
            /*if(!check_verify($captcha)){
                return ['status'=>1,'msg'=>'验证码不正确，请重新输入！'];
            }*/

            $admin_user_model = new UserModel();

            $user_info = $admin_user_model->findUser($name, $pwd);

            //show_bug($user_info['password']."---input=".password($pwd, $user_info['encrypt']));die;
            if(!$user_info ){
                return ['status'=>1,'msg'=>'用户名不存在！'];
            }
            if ($user_info['status'] === 1){
                return ['status'=>1,'msg'=>'该用户被禁止登录！'];
            }
            if (password($pwd, $user_info['encrypt']) != $user_info['password']) {
                return ['status'=>1,'msg'=>'用户名或密码不正确，请重新输入！'];
            }

            session('user_info', $user_info);
            session('user_info')['current_time'] = date("Y-m-d H:i:s",time());
            $admin_user_model->updateLoginTime($user_info['id']);

            return ['status'=>0,'msg'=>'登录成功！','url'=>url('Index/index')];
        }

        return $this->view->fetch('login');
    }




    /**
     * 退出登录
     */
    public function logout()
    {
        session_unset();
        Session::delete("user_info");
        $this->redirect('Login/index');
    }

}